/* Copyright © Worklight Ltd. 2006-2011.  All rights reserved. */

package com.worklight.androidgap.plugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.worklight.androidgap.WLAndroidUtils;
import com.worklight.androidgap.WLDroidGap;

public class WLOptionsMenu {
		
	private boolean isInit     = false;
	private boolean isEnabled  = false;
	private boolean isVisible  = false;
	private boolean hasChanged = false;
	private int		itemIndexer = 0;
	private String	skinName	= null;
	private WLDroidGap activity = null;
	
	private Map<Integer, WLMenuItem> items = null;	
	
	public WLOptionsMenu(WLDroidGap activity) {
		super();
		this.activity = activity;
	}

	public void init(){
		this.skinName = WLAndroidUtils.readWLPref(this.activity, WLDroidGap.SKIN_NAME_PREF_KEY);
		isVisible  = true;
		isEnabled  = true;		
		hasChanged = false;		
		items      = new HashMap<Integer, WLMenuItem>();
		itemIndexer= 0;
		isInit     = true;
	}
	
	public boolean isInit(){
		return isInit;
	}
		
	public WLMenuItem addItem(String javascriptId, String javascriptCallback, String title, String imagePath, boolean isEnabled){
		
		if (getItemByJavaScriptId(javascriptId) != null){
			WLAndroidUtils.error("Android menu item id '" + javascriptId + "' already exists.");
			return null;
		}
		
		int id = itemIndexer;
		itemIndexer++;
		
		WLMenuItem b = new WLMenuItem(id, javascriptId, javascriptCallback, title, imagePath, isEnabled);
		items.put(id, b);		
		setChanged();
		
		return b;
	}

	public void removeItem(String javascriptId){
		WLMenuItem btn = getItem(javascriptId);
		if (btn != null){
			items.remove(btn.getId());
			setChanged();		
		}		
	} 
	
	public void removeItems(){
		items.clear();
		setChanged();		
	}
	
	public void setEnabled(boolean isEnabled){
		this.isEnabled = isEnabled;
		setChanged();
	}
	
	public boolean isEnabled(){
		return isEnabled;		
	}
	
	public void setVisible(boolean isVisible){
		this.isVisible = isVisible;
		setChanged();
	}
	
	public boolean isVisible(){
		return isVisible;
	}
	
	public List<WLMenuItem> getItems(){
		List<WLMenuItem> buttonList = new ArrayList<WLMenuItem>(items.values());	
		Collections.sort(buttonList, new Comparator<WLMenuItem>() {
			@Override
			public int compare(WLMenuItem item1, WLMenuItem item2) {
				return item1.getId() - item2.getId();
			}
		});
		return buttonList;
	}

	public WLMenuItem getItemById(int id){		
		WLMenuItem btn = items.get(id);
		return btn;
	}

	public WLMenuItem getItem(String javascriptId){
		WLMenuItem btn = getItemByJavaScriptId(javascriptId);
		if (btn == null){
			WLAndroidUtils.error("Android menu item id " + javascriptId + " doesn't exist");
		}
		return btn;
	}
	
	public boolean hasChanged(){		
		if (hasChanged){
			return true;
		}
		for (WLMenuItem b: items.values()){
			if (b.hasChanged()){
				return true;
			}
		}
		return false;
	}
		
	public void unsetChanged(){
		hasChanged = false;
		for (WLMenuItem b: items.values()){
			b.unsetChanged();
		}
	}
	
	private WLMenuItem getItemByJavaScriptId(String javascriptId){
		WLMenuItem item = null;
		for (WLMenuItem tempItem : items.values()){
			if (tempItem.getJavaScriptId().equals(javascriptId)){
				item = tempItem;
				break;
			}
		}
		return item;
	}
	
	private void setChanged(){
		hasChanged = true;
	} 		

	public String getSkinName() {
		return skinName;
	}
}
